<?php

namespace App\Admin\Controllers\User;

use App\Http\Controllers\Controller;
use App\Model\User;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class UserController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('用户');
            $content->description('用户管理');

            $content->body($this->grid());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(User::class, function (Grid $grid) {
            $grid->user_id('ID')->sortable();
            $grid->headimgurl('头像')->display(function ($headimgurl) {
                return "<img class='header_img' style='width:50px;height:50px;' src='" . $headimgurl . "' alt=''>";
            });
            $grid->nickname('昵称');
            $grid->sex("性别")->display(function ($sex) {
                if ($sex == 1)
                    return "男";
                elseif ($sex == 2)
                    return "女";
                else
                    return "未知";
            });
            $grid->balance('余额');
            $grid->country('国家');
            $grid->province('省份');
            $grid->city('城市');
            $grid->openid('openid');
            $grid->created_at("创建时间");
            $grid->updated_at("更新时间");
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('用户');
            $content->description('用户管理');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(User::class, function (Form $form) {
            $form->display('user_id', 'user_id');
            $form->display('openid', 'openid');
            $form->display('nickname', 'nickname');
            $form->display('sex', 'sex');
            $form->display('country', 'country');
            $form->display('province', 'province');
            $form->display('city', 'city');
            $form->display('headimgurl', 'headimgurl');
            $form->display('created_at', 'created_at');
            $form->display('updated_at', 'updated_at');
            $form->display('balance', 'balance');
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('用户');
            $content->description('用户管理');

            $content->body($this->form());
        });
    }
}

